<template>
  <p class="login">
    <el-tabs v-model="activeName">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="管理员账户" prop="name"
          ><el-input v-model="ruleForm.name"></el-input
        ></el-form-item>

        <el-form-item label="密码" prop="pass"
          ><el-input
            type="password"
            v-model="ruleForm.pass"
            auto-complete="off"
          ></el-input
        ></el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >

          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- <el-tab-pane label="注册" name="second">
        <register></register>
      </el-tab-pane> -->
    </el-tabs>
  </p>
</template>
  
<script>
// import register from "@/components/Register";
import { admin } from "../api/index";
import { setToken } from "../utils/auth";
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }

        callback();
      }
    };

    return {
      activeName: "first",
      ruleForm: {
        name: "",
        pass: "",
        checkPass: "",
      },
      res: {
        username: "",
        password: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入您的名称", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 2 到 5 个字符", trigger: "blur" },
        ],
        pass: [{ required: true, validator: validatePass, trigger: "blur" }],
      },
    };
  },

  methods: {
    //选项卡切换
    // handleClick(tab, event) {},
    //重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //登录按钮
    submitForm() {
      var uname = this.ruleForm.name;
      var pword = this.ruleForm.pass;

      admin({ AdminUsername: uname, AdminPassword: pword })
        .then(({ data }) => {
          var adminid = data.data[0].id;
          if (data.code === 200) {
            sessionStorage.setItem("token", "true");
            sessionStorage.setItem("adminId", adminid);
            setToken(data.data[0].token, data.data[0].refreshToken);
            this.$router.push({path:'/backstage'})
          } else {
            alert("用户名密码错误");
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
  mounted() {},
  components: {
    // register,
  },
};
</script>
  
<style >
.login {
  width: 400px;
  margin: 0 auto;
}

.el-tabsitem {
  text-align: center;
  width: 60px;
}
</style>